<?php
/**
 * 内容管理
 * @author tangbl
 *
 */
class ContentAction extends CommonAction {
 
    /**
     * 初始化
     */
    public function _initialize(){
    
    	parent::_initialize();
    	//parent::checkTimestamp();
    }
    
    /**
     * 内容分页列表
     * @param string $table 内容表名
     * @param array  $where 查询条件数组
     */
    public function contentList(){
    	
    	$table   = $this->_req('table','ucfirst');
    	$desc    = $this->_req("orderDirection","trim","desc");;
    	$order   = $this->_req("orderName","trim","sort");
    	$pageNum = $this->_req("pageNum","intval",C('PAGE_NUMS'));
    	$nowPage = $this->_req("p","intval",1);
    	$sort    = $order.' '.$desc.',addtime desc';

    	$map   = $this->_req('where');
    	//Log::write(var_export($map,true),Log::DEBUG);
    	
    	$model = M($table);
    	$count = $model->where($map)->count();
    	$list  = $model->where($map)->order($sort)->page($nowPage.','.$pageNum)->select();
    	Log::write($model->getLastSql(),Log::DEBUG);
    	
    	$data  = array("count"=>$count, "list"=>$list);
    	$this->success($data);
    	
    }
    
    /**
     * 内容添加
     * @param string $table 内容表名
     * @param array  $image 上传图片数组array('single'=>array('single'),'atlas'=>=>array('atlas'))
     * @access public
     */
    public function contentAdd(){
    	
    	$table = $this->_req('table','ucfirst');
    	$image = $this->_req('image');
    	
    	$param = array(
    		'upload' => $image
    	);
    	parent::do_add("$table", $param);
    }
    
    
    /**
     * 内容修改
     * @param string $table 内容表名
     * @param array  $image 上传图片数组array('single'=>array('single'),'atlas'=>=>array('atlas'))
     * @access public
     */
    public function contentEdit(){
    	
    	$table = $this->_req('table','ucfirst');
    	$image = $this->_req('image');
    	
    	$param = array(
    		'upload' => $image
    	);
    	parent::do_edit("$table", $param);
    }
	
	/**
     * 内容详情
     * @param string $table 内容表名
     * @param array  $image 上传图片数组array('single'=>array('single'),'atlas'=>=>array('atlas'))
     * @access public
     */
    public function contentDetail(){
    	
		$id    = $this->_req('id');  
    	$table = $this->_req('table','ucfirst');
    	$image = $this->_req('image');
    	
		if($id){    		
			
    		$data = M($table)->find($id);
			if(is_array($image)){			
				if(is_array($image['single'])){
					foreach($image['single'] as $v){
						$data['image']['single'][$v] = find_thumb($data['solevar'],$v,$table);
					}
				}
			
				if(is_array($image['atlas'])){
					foreach($image['atlas'] as $v){
						$data['image']['atlas'][$v] = find_album($data['solevar'],$v,$table);
					}
				}
			
				if(is_array($image['attach'])){
					foreach($image['attach'] as $v){
						$data['image']['attach'][$v] = find_file($data['solevar'],$v,$table);
					}
				}
			}
			
    		$this->success($data);
    	}else{
    		$this->error();
    	}
    }
    
    /**
     * 内容删除
     * @param string $table 内容表名
     * @param int    $isdel 物理删除:1|逻辑删除0  
     * @access public
     */
    public function contentDel(){
    	
    	$isdel = $this->_req('isdel','intval',0);
    	$table = $this->_req('table','ucfirst');
    	parent::do_del("$table",$isdel);
    }
    
    
    /**
     * 排序
     * @param  string $table 内容表名
     * @param  array  $sort  排序数值数组
     * @access public
     * @return array
     */
    public function contentSort(){
    	 
    	$table  = $this->_req('table','ucfirst');
    	$model  = M($table);
    	$sorts  = $this->_req('sort');
    	 
    	foreach ($sorts as $id => $sort) {
    		$data = $model->save(array('id'=>$id,'sort'=>$sort));
    	}
    	if($data){
    		$this->success($data);
    	}else{
    		$this->error("排序失败");
    	}
    }
    
    /**
     * 查询数据量
     */
    public function contentCount(){
    	 
    	$table = $this->_req('table','ucfirst');
    
    	$map   = $this->_req('where');
    	if(is_array($map)){
    		$map = array_filter($map);
    	}
    	$model = D($table);
    	$list  = $model->where($map)->count();
    	//Log::write($model->getLastSql(),Log::DEBUG);
    	 
    	$this->success($list);
    }
    
    
    /**
     * 内容批量处理
     * @param string $table 内容表名
     * @param string $id    以逗号分割的id字符串或者数组
     * @param string $field 处理字段
     * @param string $value 处理字段的值
     * @access public
     */
    public function batch(){

    	$field = $this->_req('field','trim');
    	$value = $this->_req('value','trim');
    	$table = $this->_req('table','ucfirst');
    	$ids   = $this->_req('id');
    	if(!is_array($ids)){
    		$ids = explode(',', $ids);
    	}
    	$save[$field] = $value;
    	$where['id']  = array('in',$ids);
    	$flag = M($table)->where($where)->save($save);
    	if($flag){
    		$this->success('批量处理成功');
    	}else{
    		$this->error('批量处理失败');
    	}
    }
    
    /**
     * 批量审核
     * @param string $table 内容表名
     * @param string $id    以逗号分割的id字符串或者数组
     * @param string $field 处理字段
     * @param string $value 处理字段的值
     * @access public
     */
    public function check(){
    	
    	$field = $this->_req('field','trim');
    	$value = $this->_req('value','trim');
    	$table = $this->_req('table','ucfirst');
    	$ids   = $this->_req('id');
    	if($ids){
    		$idArr = explode(',',$ids);
    		$map['checked'] = $value;
    		foreach($idArr as $k=>$v){
    			$map['id'] = $v;
    			$data = M($table)->save($map);
    			Log::write('sql:'.M($table)->getLastSql().':'.var_export($map,true),Log::DEBUG);
    		}
    	}
    	if($data){
    		$this->success('批量处理成功');
    	}else{
    		$this->error('批量处理失败');
    	}
    }
   
}